package com.celebriosoftware.client.listener;

import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.util.Log;

import com.celebriosoftware.client.MainActivity;
import com.celebriosoftware.client.fragments.DesktopFragment;

public class MyPhoneStateListener extends PhoneStateListener {
	private int gsmState = 4;
	private DesktopFragment fragment = null;

	public MyPhoneStateListener() {
	}

	public MyPhoneStateListener(DesktopFragment fragment) {
		this.fragment = fragment;
	}

	@Override
	public void onSignalStrengthsChanged(SignalStrength signalStrength) {
		super.onSignalStrengthsChanged(signalStrength);
		int strength = signalStrength.getGsmSignalStrength();
		if (strength < 4) {
			gsmState = 1;
		} else if (strength < 8) {
			gsmState = 2;
		} else if (strength < 16) {
			gsmState = 3;
		} else if (strength < 32) {
			gsmState = 4;
		} else {
			gsmState = 0;
		}
		reportGsmState();
	}
	private void reportGsmState() {
		if(fragment != null) {
			fragment.updateGSMState(gsmState);
		}
		else {
			Log.w("gsm listener", "parent activity not supplied");
		}
	}	

	public int getGsmState() {
		return gsmState;
	}
}
